package com.theeviljames.ac.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import com.theeviljames.ac.exceptions.FileRWException;

public class FileRW {

	private static FileRW _instance;
	private ArrayList<String> openFiles = new ArrayList<String>();
	
	private FileRW(){
		
	}
	
	public static FileRW getInstance(){
		if(_instance==null)_instance = new FileRW();
		return _instance;
	}
	
	public BufferedReader openFile(String filename) throws FileRWException{
		if(openFiles.contains(filename)) throw new FileRWException("The file is already opened");
		openFiles.add(filename);
		File f = new File(filename);
		if(!f.exists())throw new FileRWException("The file does not appear to exists. Note: File names must include full path");
		BufferedReader br = null;
		try{
			br = new BufferedReader(new FileReader(f));
		}
		catch(Exception e){
			throw new FileRWException("There was an error trying to construct a new buffered file reader");
		}
		return br;
	}
	
	public static void main(String[] args) {
		try{
			BufferedReader br = new BufferedReader(new FileReader(new File("com\\theeviljames\\ac\\lib\\test.txt")));
			String s = br.readLine();
			while(s!=null){
				System.out.println(s); 
				s = br.readLine();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}
